package com.souku.finance.application.base;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.pm.ActivityInfo;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.WindowManager;

import com.souku.finance.application.R;
import com.souku.finance.application.ui.MainActivity;
import com.souku.finance.application.utils.Constant;
import com.souku.finance.application.ui.lock.LockActivity;
import com.souku.finance.application.ui.lock.LockSetupActivity;
import com.souku.finance.application.utils.ShareUtil;
import com.souku.finance.application.utils.ToastUtil;
import com.souku.finance.application.utils.ViewUtil;
import com.souku.finance.application.http.volley.ServiceTimeHelper;
import com.umeng.analytics.MobclickAgent;

/**
 * Created by Android on 2016/12/5.
 */

public abstract class BaseActivity extends Activity {


    public final int REQUEST = 1;
    public final int DEFAULT = 2;
    private boolean isRegister = true;
    private static BaseActivity foregroundActivity;

    public Handler mhandle = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case REQUEST:
                    IntentFilter mFilter = new IntentFilter();
                    mFilter.addAction(ConnectivityManager.CONNECTIVITY_ACTION);
                    registerReceiver(myNetReceiver, mFilter);
                    break;
                case DEFAULT:
                    mhandleCallback();
                    break;
            }
        }
    };

    public void mhandleCallback() {

    }

    public static BaseActivity getForegroundActivity(){
        return foregroundActivity;
    }

    @Override
    public void setContentView(int layoutResID) {
        super.setContentView(layoutResID);
        ViewUtil.setColor(this, getResources().getColor(R.color.main));
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
        }
        ActivityManage.add(this);
        ActivityManage.finishSameLesOne(getClass());
        mhandle.sendEmptyMessage(REQUEST);
    }

    @Override
    protected void onResume() {
        //获取服务器时间
        new ServiceTimeHelper(this).getTime();
        MainActivity.isBackstage = false;
        //竖屏
        if (getRequestedOrientation() != ActivityInfo.SCREEN_ORIENTATION_PORTRAIT) {
            setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        }
//        if (ShareUtil.getInstance().getBoolean(Constant.IsLogin)) {
//            if (ShareUtil.getInstance().getString(Constant.GesturePass) == null) {
//                Intent intent = new Intent(this, LockSetupActivity.class);
//                startActivity(intent);
//            }
//        }
//        if (MainActivity.islock) {
//            if (System.currentTimeMillis() - MainActivity.lockTime > 30 * 1000) {
//                Intent intent = new Intent(this, LockActivity.class);
//                startActivity(intent);
//            }
//        }
        MainActivity.islock = false;
        super.onResume();
        MobclickAgent.onResume(this);
        foregroundActivity = this;
    }


    private ConnectivityManager mConnectivityManager;
    private NetworkInfo netInfo;
    private BroadcastReceiver myNetReceiver = new BroadcastReceiver() {

        @Override
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            if (action.equals(ConnectivityManager.CONNECTIVITY_ACTION)) {
                mConnectivityManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
                netInfo = mConnectivityManager.getActiveNetworkInfo();
                if (netInfo != null && netInfo.isAvailable()) {
                    if (!isRegister) {
                        internet();
                    }
                    View tipView = findViewById(R.id.tipview);
                    if (tipView != null) {
                        tipView.setVisibility(View.GONE);
                    }
                } else {
                    View tipView = findViewById(R.id.tipview);
                    if (tipView != null) {
                        tipView.setVisibility(View.VISIBLE);
                    } else {
                        ToastUtil.show("网络连接错误");
                    }
                }
                isRegister = false;
            }
        }
    };

    public void onPause() {
        super.onPause();
        MobclickAgent.onPause(this);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        ActivityManage.remove(this);
        if (myNetReceiver != null) {
            unregisterReceiver(myNetReceiver);
        }
        mhandle.removeCallbacksAndMessages(null);
    }

    public abstract void internet();
}
